// Copyright (C) 2012 Yury Bychenkov (http://bychenkov.com).
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//  - Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//  - Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//  - Neither the name of Yury Bychenkov nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "resourcebundle.h"
#include "numberutil.h"
#include "traits.h"

ResourceBundle& ResourceBundle::operator=(const ResourceBundle& that) {
	if (index != 0) {
		delete [] (uint8*) index;
		index = 0;
		data = 0;
	}

	indexSize = indexCapacity = that.indexSize;
	dataSize = that.dataSize;
	dataCapacity = dataSize + 8; // IntegerTraits::decode

	if (indexSize > 0) {
		uint8* buffer = new uint8 [indexCapacity * sizeof(uint32) + dataCapacity];
		index = (uint32*) buffer;
		data = buffer + indexSize * sizeof(uint32);
		memcpy(index, that.index, indexSize * sizeof(uint32));
		if (dataSize > 0) {
			memcpy(data, that.data, dataSize);
		}
	}

	return *this;
}

bool ResourceBundle::add(const void* element, uint size) {
	if (!ensureCapacity(size)) {
		return false;
	}

	index[indexSize++] = size;
	index[indexSize++] = dataSize;

	for (uint i = 0; i < size; i++) {
		data[dataSize + i] = ((const uint8*) element)[i];
	}

	dataSize += size;

	return true;
}

bool ResourceBundle::addNull() {
	if (!ensureCapacity(1)) {
		return false;
	}

	index[indexSize++] = 1;
	index[indexSize++] = dataSize;

	data[dataSize] = 0xFF;

	dataSize++;

	return true;
}

bool ResourceBundle::addString(const void* element, uint size) {
	if (!ensureCapacity(size + StringTraits::MAX_OVERHEAD_SIZE)) {
		return false;
	}

	int encodedSize = StringTraits::encode((const uint8*) element, size, data + dataSize, dataCapacity - dataSize);
	if (encodedSize <= 0) {
		return false;
	}

	index[indexSize++] = (uint) encodedSize;
	index[indexSize++] = dataSize;

	dataSize += encodedSize;

	return true;
}

bool ResourceBundle::addInteger(int64 value) {
	if (!ensureCapacity(IntegerTraits::MAX_ENCODED_SIZE)) {
		return false;
	}

	uint encodedSize = IntegerTraits::encode(value, data + dataSize);

	index[indexSize++] = encodedSize;
	index[indexSize++] = dataSize;

	dataSize += encodedSize;

	return true;
}

bool ResourceBundle::addDecimal(const void* element, uint size) {
	if (!ensureCapacity(size + DecimalTraits::MAX_OVERHEAD_SIZE)) {
		return false;
	}

	int encodedSize = DecimalTraits::encode((const uint8*) element, size, data + dataSize, dataCapacity - dataSize);
	if (encodedSize <= 0) {
		return false;
	}

	index[indexSize++] = (uint) encodedSize;
	index[indexSize++] = dataSize;

	dataSize += encodedSize;

	return true;
}


bool ResourceBundle::ensureCapacity(uint size) {
	if (indexSize < indexCapacity && dataSize + size <= dataCapacity) {
		return true;
	}

	uint newIndexCapacity = (indexCapacity - indexSize < 8? indexCapacity + 16 : indexCapacity);
	uint newDataCapacity = ((dataCapacity + size + 4095U) & ~4095U);
	uint8* buffer = new uint8[newIndexCapacity * sizeof(uint32) + newDataCapacity];

	if (indexSize > 0) {
		memcpy(buffer, index, indexSize * sizeof(uint32));
	}

	if (dataSize > 0) {
		memcpy(buffer + newIndexCapacity * sizeof(uint32), data, dataSize);
	}

	if (index != 0) {
		delete [] (uint8*) index;
	}

	indexCapacity = newIndexCapacity;
	dataCapacity = newDataCapacity;
	index = (uint32*) buffer;
	data = buffer + indexCapacity * sizeof(uint32);

	return true;
}
